import { defineStore } from "pinia";
import $web3 from "@/web3/bsc_tools";

export default defineStore("user", {
  state: () => ({
    address: "",
    token: "",
    isSign: false,
    sign_text: '',
    user_info: {},
  }),
  getters: {
    isLogin: (state) => !!state.token, // 是否登录（不是钱包）
  },
  actions: {
    setToken(val) {
      this.token = val;
    },
    async connectWallet() {
      try {
        const {flag,sign} = await $web3.authorizeSignature("trade");
        this.isSign = flag;
        this.sign_text = sign;
        const res = await $web3.connectWallet();
        this.address = res;
      } catch (error) {
        throw new Error(error);
      }
    },
    // 重置状态
    reset() {
      this.address = "";
      this.token = "";
      this.isSign = false;
      this.sign_text = '';
      this.user_info = {};
    },
  },
  persist: {
    storage: sessionStorage,
  },
});
